const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        searchValue: "",
        waitReceiveList: [],
        isLastPage: false,
        current: 1,
        size: 10,
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.getData()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    pageLifetimes: {
        show: function () {
            this.onPullDownRefresh()
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onPullDownRefresh() {
            this.setData({
                current: 1,
                searchValue: "",
                waitReceiveList: [],
                isLastPage: false
            })
            this.getData()
        },
        onReachBottom() {
            let {
                current,
                isLastPage
            } = this.data
            if (isLastPage) {
                return
            }
            this.setData({
                current: (Number(current) + 1)
            })
            this.getData()
        },
        onSearch() {
            this.setData({
                current: 1,
                waitReceiveList: [],
                isLastPage: false
            })
            this.getData()
        },
        toPurchaserDetail(e) {
            let item = e.currentTarget.dataset.item
            let title = `${item.name} ${item.mobile}`
            wx.navigateTo({
                url: `/pages/payment/wait-receive/index?id=${item.userId}&title=${title}`,
            })
        },
        getData() {
            const {
                role,
                searchValue,
                current,
                size
            } = this.data;
            let params = {
                settleIs: false,
                current,
                size,
                keyword: searchValue,
                companyId: app.globalData.userInfo.companyData.companyId,
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis.getOutStockSettlementPage(params).then(res => {
                wx.stopPullDownRefresh();
                let oldWaitReceiveList = this.data.waitReceiveList;
                let waitReceiveList = res.data && res.data.records.map(item => {
                    item.settleAccountPrice = item.settleAccount && wx.$getYuan(item.settleAccount)
                    return item
                })
                if (waitReceiveList && waitReceiveList.length) {
                    let newWaitReceiveList = oldWaitReceiveList.concat(waitReceiveList)
                    this.setData({
                        waitReceiveList: newWaitReceiveList
                    })
                    if (waitReceiveList.length < size) {
                        this.setData({
                            isLastPage: true
                        })
                    }
                } else {
                    this.setData({
                        isLastPage: true
                    })
                }
            })
        }
    }
})